package com.atguigu.edu.controller.front;

import com.atguigu.commonUtils.R;
import com.atguigu.edu.entity.Course;
import com.atguigu.edu.entity.Teacher;
import com.atguigu.edu.service.CourseService;
import com.atguigu.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/edu/teacherFront")
//@CrossOrigin
public class TeacherFrontController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private CourseService courseService;

    /**
     * 分页查询讲师的方法
     *
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("getTeacherFrontList/{page}/{limit}")
    public R getTeacherFrontList(@PathVariable long page, @PathVariable long limit) {
        Page<Teacher> pageTeacher = new Page<>(page, limit);

        Map<String, Object> map = teacherService.getTeacherFrontList(pageTeacher);

        //返回分页中的所有数据
        return R.ok().data(map);
    }

    /**
     * 查询讲师详情
     *
     * @param teacherId
     * @return
     */
    @GetMapping("getTeacherFrontInfo/{teacherId}")
    public R getTeacherFrontInfo(@PathVariable String teacherId) {
        //1.根据id查询讲师基本信息
        Teacher teacher = teacherService.getById(teacherId);
        //2.根据讲师id查询所讲课程

        List<Course> courseList = courseService.list(new QueryWrapper<Course>().eq("teacher_id", teacherId));

        return R.ok().data("teacher", teacher).data("courseList", courseList);
    }


}
